#!/bin/bash
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Download the newest version of Closure Compiler (pre-compiled), and put into
# Chrome source tree. Also download latest versions of various externs files.

readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly TEMP_DIR=$(mktemp -d)
readonly EXTERNS_NPM_DIR="node_modules/google-closure-compiler/contrib/externs"
readonly EXTERNS_DIR="${SCRIPT_DIR}/externs"
readonly README="${SCRIPT_DIR}/README.chromium"

cleanup() {
  rm -rf "${TEMP_DIR}"
}

get_sha1() {
  sha1sum "$1" | cut -d" " -f1
}

trap cleanup SIGINT SIGHUP SIGTERM

old_chrome_sha1=$(get_sha1 "${EXTERNS_DIR}/chrome.js")
old_extensions_sha1=$(get_sha1 "${EXTERNS_DIR}/chrome_extensions.js")
old_polymer_sha1=$(get_sha1 "${EXTERNS_DIR}/polymer-1.0.js")
old_mocha_sha1=$(get_sha1 "${EXTERNS_DIR}/mocha-2.5.js")

cd "${TEMP_DIR}"

# Check if NPM is installed.
command -v npm >/dev/null 2>&1 || {
  echo >&2 "NPM is required to run this script. Aborting.";
  exit 1;
}

echo "Downloading Closure Compiler latest..."
npm install --prefix . --no-bin-links --only=prod google-closure-compiler

json_file=node_modules/google-closure-compiler-java/package.json
new_version=$(cat $json_file | grep version | cut -f2 -d ":" | tr -d '" ')
new_date="$(date --utc '+%Y/%m/%d %H:%M')"

jar_file=node_modules/google-closure-compiler-java/compiler.jar
cp "${jar_file}" "${SCRIPT_DIR}/compiler/compiler.jar"

readonly WARNING="$(cat <<EOT
//    SSSSSSSSSSSSSSS TTTTTTTTTTTTTTTTTTTTTTT     OOOOOOOOO     PPPPPPPPPPPPPPPPP
//  SS:::::::::::::::ST:::::::::::::::::::::T   OO:::::::::OO   P::::::::::::::::P
// S:::::SSSSSS::::::ST:::::::::::::::::::::T OO:::::::::::::OO P::::::PPPPPP:::::P
// S:::::S     SSSSSSST:::::TT:::::::TT:::::TO:::::::OOO:::::::OPP:::::P     P:::::P
// S:::::S            TTTTTT  T:::::T  TTTTTTO::::::O   O::::::O  P::::P     P:::::P
// S:::::S                    T:::::T        O:::::O     O:::::O  P::::P     P:::::P
//  S::::SSSS                                                     P::::PPPPPP:::::P
//   SS::::::SSSSS       This file is generated. To update it,    P:::::::::::::PP
//     SSS::::::::SS          run roll_closure_compiler.          P::::PPPPPPPPP
//        SSSSSS::::S                                             P::::P
//             S:::::S        T:::::T        O:::::O     O:::::O  P::::P
//             S:::::S        T:::::T        O::::::O   O::::::O  P::::P
// SSSSSSS     S:::::S      TT:::::::TT      O:::::::OOO:::::::OPP::::::PP
// S::::::SSSSSS:::::S      T:::::::::T       OO:::::::::::::OO P::::::::P
// S:::::::::::::::SS       T:::::::::T         OO:::::::::OO   P::::::::P
//  SSSSSSSSSSSSSSS         TTTTTTTTTTT           OOOOOOOOO     PPPPPPPPPP
EOT
)"

(echo "${WARNING}" && cat ${EXTERNS_NPM_DIR}/chrome.js) > "${EXTERNS_DIR}/chrome.js"
(echo "${WARNING}" && cat ${EXTERNS_NPM_DIR}/chrome_extensions.js) > "${EXTERNS_DIR}/chrome_extensions.js"
(echo "${WARNING}" && cat ${EXTERNS_NPM_DIR}/polymer-1.0.js) > "${EXTERNS_DIR}/polymer-1.0.js"
(echo "${WARNING}" && cat ${EXTERNS_NPM_DIR}/mocha-2.5.js) > "${EXTERNS_DIR}/mocha-2.5.js"

# Apply local Chromium patch.
cd "${SCRIPT_DIR}"
git apply chromium_patch.diff

new_chrome_sha1=$(get_sha1 "${EXTERNS_DIR}/chrome.js")
if [[ "${new_chrome_sha1}" != "${old_chrome_sha1}" ]]; then
  chrome_range="chrome.js: ${old_chrome_sha1} -> ${new_chrome_sha1}"
fi

new_extensions_sha1=$(get_sha1 "${EXTERNS_DIR}/chrome_extensions.js")
if [[ "${new_extensions_sha1}" != "${old_extensions_sha1}" ]]; then
  extensions_range="chrome_extensions.js: ${old_extensions_sha1} -> ${new_extensions_sha1}"
fi

new_polymer_sha1=$(get_sha1 "${EXTERNS_DIR}/polymer-1.0.js")
if [[ "${new_polymer_sha1}" != "${old_polymer_sha1}" ]]; then
  polymer_range="polymer-1.0.js: ${old_polymer_sha1} -> ${new_polymer_sha1}"
fi

new_mocha_sha1=$(get_sha1 "${EXTERNS_DIR}/mocha-2.5.js")
if [[ "${new_mocha_sha1}" != "${old_mocha_sha1}" ]]; then
  mocha_range="mocha-2.5.js: ${old_mocha_sha1} -> ${new_mocha_sha1}"
fi

echo
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
echo "@"
echo "@  ROLL RESULTS:"
echo "@"
echo "@  closure-compiler-latest.zip version:"
echo "@    Old: ${old_version}"
echo "@    New: ${new_version}"
echo "@"
echo "@  externs/chrome.js SHA1:"
echo "@    Old: ${old_chrome_sha1}"
echo "@    New: ${new_chrome_sha1}"
echo "@"
echo "@  externs/chrome_extensions.js SHA1:"
echo "@    Old: ${old_extensions_sha1}"
echo "@    New: ${new_extensions_sha1}"
echo "@"
echo "@  externs/polymer-1.0.js SHA1:"
echo "@    Old: ${old_polymer_sha1}"
echo "@    New: ${new_polymer_sha1}"
echo "@"
echo "@  externs/mocha-2.5.js SHA1:"
echo "@    Old: ${old_mocha_sha1}"
echo "@    New: ${new_mocha_sha1}"
echo "@"
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
echo

sed -i "s,^Date: .*,Date: ${new_date},; s,^Version: .*,Version: ${new_version}," "${README}"

echo "git commit -a -m 'Roll closure compiler"
echo

if [[ ! -z "${chrome_range}" ]]; then echo "${chrome_range}"; fi
if [[ ! -z "${extensions_range}" ]]; then echo "${extensions_range}"; fi
if [[ ! -z "${polymer_range}" ]]; then echo "${polymer_range}"; fi
if [[ ! -z "${mocha_range}" ]]; then echo "${mocha_range}"; fi
echo
echo "TBR="
echo "BUG='"
echo
echo "git cl upload"

cleanup
